
#import "UIViewController+QDSwizzling.h"
#import <objc/runtime.h>

@implementation UIViewController (QDSwizzling)

+ (void)load
{
    static dispatch_once_t once_Token;
    dispatch_once(&once_Token, ^ {
        Class class = [self class];
        
        //为了修改bug的时候，方便找到界面对应的视图控制器
        Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
        Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:));
        method_exchangeImplementations(viewWillAppear, logViewWillAppear);
        
        //viewDidAppear:
        SEL originalSelector = @selector(viewDidAppear:);
        SEL swizzledSelector = @selector(swizzlingViewDidAppear:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (didAddMethod) {
            class_addMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
        
        //viewDidDisappear:
        SEL originalDisSelector = @selector(viewDidDisappear:);
        SEL swizzledDisSelector = @selector(swizzlingViewDidDisappear:);
        
        Method originalDisMethod = class_getInstanceMethod(class, originalDisSelector);
        Method swizzledDisMethod = class_getInstanceMethod(class, swizzledDisSelector);
        
        BOOL didDisAddMethod = class_addMethod(class, originalDisSelector, method_getImplementation(swizzledDisMethod), method_getTypeEncoding(swizzledDisMethod));
        if (didDisAddMethod) {
            class_addMethod(class, swizzledDisSelector, method_getImplementation(originalDisMethod), method_getTypeEncoding(originalDisMethod));
        } else {
            method_exchangeImplementations(originalDisMethod, swizzledDisMethod);
        }
    });
}

// 自己实现的方法，也就是和self的viewDidAppear方法进行交换的方法。
- (void)swizzlingViewDidAppear:(BOOL)animated
{
    [self swizzlingViewDidAppear:animated];
}
// 自己实现的方法，也就是和self的viewDidDisappear方法进行交换的方法。
- (void)swizzlingViewDidDisappear:(BOOL)animated
{
    
    [self swizzlingViewDidDisappear:animated];
}

- (void)logViewWillAppear:(BOOL)animated {

    NSString *className = NSStringFromClass([self class]);
    if ([className hasPrefix:@"UI"] == NO) {
        NSLog(@"界面对应的视图控制器是%@",className);
    }
    
    [self logViewWillAppear:animated];
    
}

#pragma mark 提示框
- (void)alertAction:(NSString *)alertString
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:alertString preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
